package com.fox0g.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fox0g.mapper.*;
import com.fox0g.netty.NettyServerHandler;
import com.fox0g.pojo.*;
import com.fox0g.service.FarmService;
import com.fox0g.service.VirusService;
import com.fox0g.websocket.WebSocketHandler;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
* @author 86176
* @description 针对表【virus(病毒含量)】的数据库操作Service实现
* @createDate 2024-07-10 09:13:25
*/
@Service
public class VirusServiceImpl implements VirusService{
    ArrayList<String> list = new ArrayList<>();
    //    @Autowired
//    ShengxiangService shengxiangService;
    @Autowired
    FarmService farmService;

    @Autowired
    FarmMapper farmMapper;


    @Autowired
    WebSocketHandler webSocketHandler;
    @Autowired
    NettyServerHandler nettyServerHandler;
    @Autowired
    FreshfoodMapper freshfoodMapper;

    @Autowired
    SharebikeMapper sharebikeMapper;

    @Autowired
    TstateMapper tstateMapper;

    @Autowired
    ProfitMapper profitMapper;


    @Autowired
    VirusMapper virusMapper;

    @Autowired
    GlsMapper glsMapper;

    @Autowired
    InoroutMapper inoroutMapper;


    public static String Meg = "";

    @Override
    public void HandleMessage(String message) throws IOException {
        webSocketHandler.send(message);
//        解析温湿度光照强度数据帧
        //HWV122.0I12TS6T123.3T221.0Z
        if (message.matches("HWp1\\d+(\\.\\d+)?p2\\d+(\\.\\d+)?bd\\d+(\\.\\d+)?t\\d+(\\.\\d+)?Z")) {

            String arrnews1[] = message.split("p1");
            String arrnew2[] = arrnews1[1].split("p2");
            String arrnew3[] = arrnew2[1].split("bd");
            String arrnew4[] = arrnew3[1].split("t");
            String arrnew5[] = arrnew4[1].split("Z");
//            String arrnew6[] = arrnew5[1].split("Z");
            String V1 = arrnew2[0];  //气压1
            String I = arrnew3[0];  //气压2
            String TS = arrnew4[0]; //病毒含量
            String T1 = arrnew5[0]; //室温
//            String T2 = arrnew6[0]; //饮水水质
            System.out.println(V1 + " " + I + " " + TS + " " + T1 + " ");
            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("P1" + V1);
            webSocketHandler.send("P2" + I);
            webSocketHandler.send("VIRUS" + TS);
            webSocketHandler.send("T#" + T1);
            System.out.println(V1 + " " + TS + " " + T1 + " ");
//            webSocketHandler.send("sw" + TS);
            //将解析后的数据设置给pojo类

            Gls gls = new Gls();
            gls.setP1(V1);
            gls.setP2(I);
            gls.setWd(T1);
            gls.setVirus(TS);
            gls.setCreateTime(now);
            glsMapper.insert(gls);


            Virus virus = new Virus();
            virus.setCreateTime(now);
            virus.setVirusvolum(TS);
            virusMapper.insert(virus);
            System.out.println("病毒含量更新成功!");

        }


        //HWV122.0I12TS6T123.3T221.0Z
        if (message.matches("HWt1\\d+(\\.\\d+)?t2\\d+(\\.\\d+)?Z")) {

            String arrnews1[] = message.split("t1");
            String arrnew2[] = arrnews1[1].split("t2");
            String arrnew3[] = arrnew2[1].split("Z");
//            String arrnew6[] = arrnew5[1].split("Z");
            String V1 = arrnew2[0];  //气压1
            String I = arrnew3[0];  //气压2
//            String T2 = arrnew6[0]; //饮水水质
            System.out.println(V1 + " " + I + " " );
            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
            //向前端发送这些数据帧
            webSocketHandler.send("T1#" + V1);
            webSocketHandler.send("T2#" + I);





        }

        if(message.matches("HWd[0-9]+Z")){
            String money = message.substring(3,message.length() - 1);
            Profit profit = new Profit();
            LocalDate now = LocalDateTime.now().toLocalDate(); //获取当前时间
            profit.setMoney(money);
            profit.setCreatetTime(now);
            profitMapper.insert(profit);
            System.out.println("营业额插入成功！");
        }

//        if(message.matches("HWid[0-9A-Z]+dcrl\\d+(\\.\\d+)?Z")){
//
//            String arrnews1[] = message.split("id");
//            String arrnew2[] = arrnews1[1].split("dcrl");
//            String arrnew3[] = arrnew2[1].split("Z");
//
//            String V1 = arrnew2[0];  //截取温度
//            String I = arrnew3[0];  //电容
//            Battery battery = new Battery();
//            LocalTime now = LocalDateTime.now().toLocalTime(); //获取当前时间
//            battery.setDcrl(I);
//            battery.setYhid(V1);
//            battery.setCreateTime(now);
//            batteryMapper.insert(battery);
//            System.out.println("电池容量插入成功！");
//        }


        if(message.matches("HWPID[0-9A-Z]+t\\d+(\\.\\d+)?Z")){
            String arrnews1[] = message.split("PID");
            String arrnew2[] = arrnews1[1].split("t");
            String arrnew3[] = arrnew2[1].split("Z");
            String V1 = arrnew2[0];
            String I = arrnew3[0];
            webSocketHandler.send("PID" + V1 + "PT" + I);
            System.out.println("PID" + V1 + "PT" + I);
//            webSocketHandler.send("PT" + I);

        }
//        if(message.substring(0,5).equals("HWRID")){
//            String userid = message.substring(5,message.length() - 1);
//            User user = new User();
//            userMapper.deleteid(userid);
//        }
//




    }

    @Override
    public PageBean page(Integer page,String begin, String end) {
        PageHelper.startPage(page, 5);
        List<Inorout> inorouts = inoroutMapper.AllData(begin,end);
        Page<Inorout> p = (Page<Inorout>) inorouts;
        System.out.println("总记录数" + p.getTotal());
        return new PageBean(p.getTotal(), p.getResult());

    }
}




